package com.chen.controller;


import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chen.annotation.OptLog;
import com.chen.common.OptTypeConst;
import com.chen.common.Result;
import com.chen.entity.User;
import com.chen.mapper.UserMapper;
import com.chen.service.impl.UserServiceImpl;
import com.chen.vo.UserPasswordDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.baomidou.mybatisplus.core.assist.ISqlRunner.DELETE;
import static com.baomidou.mybatisplus.core.assist.ISqlRunner.UPDATE;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author personcoder007
 * @since 2022-07-17
 */
@RestController
@RequestMapping("/user")
@Api(value = "UserController", tags = "用户模块")
@SuppressWarnings("all")
public class UserController {

    @Resource
    private UserServiceImpl userService;

    @Resource
    private UserMapper userMapper;

    /**
     * 分页
     * @param pageNum
     * @param pageSize
     * @param username
     * @param nickname
     * @param address
     * @return
     */
    @GetMapping("/selectPage")
    @ApiOperation(value = "分页")
    public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize,
                             @RequestParam(defaultValue = "") String username,
                             @RequestParam(defaultValue = "") String nickname,
                             @RequestParam(defaultValue = "") String address) {

        return Result.success(userService.selectPage(pageNum, pageSize, username, nickname, address));
    }


    /**
     * 根据id进行删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @OptLog(optType = OptTypeConst.REMOVE)
    @ApiOperation(value = "删除")
    public Result delete(@PathVariable Integer id) {
        return Result.success(userService.removeById(id));
    }


    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("/del/batch")
    @OptLog(optType = OptTypeConst.REMOVE_BATH)
    @ApiOperation(value = "批量删除")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        System.out.println(ids);
        return Result.success(userService.removeByIds(ids));
    }



    // 新增或者更新
    @PostMapping
    @OptLog(optType = OptTypeConst.UPDATE)
    @ApiOperation(value = "新增或更新")
    public Result save(@RequestBody User user) {
        if (user.getId() == null && user.getPassword() == null) {  // 新增用户默认密码
//            user.setPassword( SecureUtil.md5("12356"));
            user.setPassword( "12356");
            user.setRole("ROLE_USER");
            user.setAddress("地球");
        }
        return Result.success(userService.saveOrUpdate(user));
    }

//    查询出当前角色
    @GetMapping("/role/{role}")
    @ApiOperation(value = "查询当前角色")
    public Result findUsersByRole(@PathVariable String role) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role", role);
        List<User> list = userService.list(queryWrapper);
        return Result.success(list);
    }


    @PutMapping("/updateInfo")
    @ApiOperation(value = "更新资料")
    @OptLog(optType = OptTypeConst.UPDATE)
    public Result updateInfo(@RequestBody User user){
        return Result.success(userMapper.updateById(user));

    }


    @GetMapping(value = "selectUser")
    @ApiOperation(value = "查询开始的开始")
    public Result selectUser() {
        return Result.success(userMapper.selectList(null));
    }



}

